/*
A palindromic number reads the same both ways.
The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 * 99.

Find the largest palindrome made from the product of two 3-digit numbers.
*/
def result = 0
for (int i=999; i>=100; i--) {
    for (int j=999; j>=100; j--) {
        if (isPalindrome(i*j) && i*j > result) {
            result = i*j
        }
    }
}

def isPalindrome(num) {
    String s = Integer.toString(num)
    for (int i=0; i<s.length()/2; i++) {
        if (s.substring(i, i+1) != s.substring(s.length()-1-i, s.length()-1-i+1)) {
            return false
        }
    }
    return true
}

println "result = ${result}"